import airsim
import os

# connect to the AirSim simulator
client = airsim.MultirotorClient()
client.confirmConnection()
client.enableApiControl(True)
client.armDisarm(True)

# Async methods returns Future. Call join() to wait for task to complete.
client.takeoffAsync().join()
client.moveToPositionAsync(-10, 10, -10, 5).join()


num = 100
start = 0

while start<=100:
    start = start+1


    # take images
    responses = client.simGetImages([
        airsim.ImageRequest("front-left", airsim.ImageType.DisparityNormalized, pixels_as_float=True, compress=False),
        airsim.ImageRequest("front-left", airsim.ImageType.Scene, False, compress=True),
        airsim.ImageRequest("front-right", airsim.ImageType.Scene, False, compress=True),
    ])

    if len(responses) ==3:
        print(airsim.get_pfm_array(responses[0]).shape)
        airsim.write_file(os.path.normpath('./data/left_'+str(start)+'.png'), responses[1].image_data_uint8)
        airsim.write_file(os.path.normpath('./data/right_' + str(start)+'.png'), responses[2].image_data_uint8)
        airsim.write_pfm(os.path.normpath('./data/left_disp_'+str(start)+'.pfm'), airsim.get_pfm_array(responses[0]))







print('Retrieved images: %d', len(responses))

# do something with the images
for response in responses:
    print(response.time_stamp)

    if response.pixels_as_float:
        print("Type %d, size %d" % (response.image_type, len(response.image_data_float)))
        print(airsim.get_pfm_array(response).shape)
        airsim.write_pfm(os.path.normpath('py1.pfm'), airsim.get_pfm_array(response))

    else:
        print("Type %d, size %d" % (response.image_type, len(response.image_data_uint8)))

        airsim.write_file(os.path.normpath('py1.png'), response.image_data_uint8)
